<?
$formid = extractNumberWithKeyFromArray("formId", $_REQUEST);
$documenttypecode = getDocumentTypeCodeFromFormId($formid, $db);
$parent = extractNumberWithKeyFromArray("parent", $_REQUEST);
$processFormData = extractStringWithKeyFromArray("processFormData", $_REQUEST);
if($processFormData == "YES") {
	$codesSaved = array();
	foreach ($_FILES as $filekey=>$fileValue) {
		if($_FILES[$filekey]['size'] != '0') {
			$formIdSave = $formid;
			$documentCodeSave = null;
			$_FILES['content'] = $_FILES[$filekey];
			//$_POST[content] = 
			$cmsWriter = new CmsWriter( $formIdSave, $documentCodeSave, $_POST, $authInfo, $db);
			$message = $cmsWriter->processFormData();
			$codesSaved[] = $cmsWriter->documentCode;
		}
	}

	//get the passed in return url, and set it in the session.
	$returnURL = extractStringWithKeyFromArray('returnURL', $_REQUEST);
	if (!is_null($returnURL)) {
		$_SESSION['returnURL'] = $returnURL;
	}
	
	if (is_null($returnURL)) {
		$categorycode = extractNumberWithKeyFromArray("categorycode", $_POST, 0);
		//once we get this far, we need to strip/ clear the form/document stuff from the session..
		$returnURL = CMS_HOME . "?fileNo=1&parent=".$categorycode."&formid=" . $formIdSave;
	}
	
	$documentType = getDocumentTypeForDocumentTypeCode($cmsWriter->formData['documenttypecode'], $db);
	$message->addValidationMessageForKey("CmsWrite","db_OBJECT_SAVED",CMS_STATUS_NOTICE);
	$m = "";
	foreach ($codesSaved as $codes) {
		$m .= "The '" . $documentType->documenttypedescription . "' with code ". $codes . " was saved successfully.<br>";
	}
		dialoguePage(array(	DLG_MESSAGE=> 	$m,
				DLG_LEFT_TEXT=>	DLG_EMPTY,
				DLG_LEFT_LINK=>	CMS_HOME,
				DLG_RIGHT_TEXT=>	DLG_OK,
				DLG_RIGHT_LINK=>	urldecode($returnURL) ));
}
else {

?>
<div class="cms_browse_heading">
  <div class="cms_browse_heading_label">
    <h1>Files Module</h1>
  </div>
  <div class="cms_browse_addrecord"></div>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>
<script src="/cms/html/js/multiple-file-element/multifile_compressed.js"></script>
<form class="cms_form" action="/cms/html/index.php?fileNo=168" method="post" enctype="multipart/form-data" name="Files Module" id="Files Module">
  <input type="hidden" name="processFormData" value="YES" />
  <input type="hidden" name="documenttypecode" value="<?= $documenttypecode; ?>">
  <input type="hidden" name="categorycode" value="<?= $parent; ?>">
  <input type="hidden" name="author" value="0">
  <input type="hidden" name="fileNo" value="168">
  <input type="hidden" name="formId" value="<?= $formid; ?>">
  <input type="hidden" name="returnURL" value="<?= urlencode("/cms/html/index.php?fileNo=1&formid=".$formid."&parent=".$parent); ?>">
  <div class="cms_masteredit">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="cms_masteraddedit_table" >
	  <tr>
	    <td height="1" width="160"><img src="/cms/images/spacer.gif" height="1" width="160" /></td>
		<td height="1"><img src="/cms/images/spacer.gif" height="1" width="1" /></td>
	  </tr>
	  <tr id="cms_masteredit_220_10001" class="cms_masteraddedit_table"> 
	    <td id="cms_form_label_datelogged" class="cms_form_label" >Last Updated:</td> 
		<td class="cms_form_element" align="left">
		  <span id="cms_form_datelogged" class="cms_form_textlabel" name="datelogged" ><?= date("l d F Y H:i:s"); ?></span>
		</td>
	  </tr>
	  <tr id="cms_masteredit_220_11360" class="cms_masteraddedit_table"> 
	    <td id="cms_form_label_title" class="cms_form_label" >Name:</td> 
		<td class="cms_form_element" align="left">
		  <input type="text" name="title" value="" id="cms_form_title" class="cms_form_text"></input>
		</td>
	  </tr>
	  <tr id="cms_masteredit_220_11361" class="cms_masteraddedit_table"> 
	    <td id="cms_form_label_shortdescription" class="cms_form_label" >Description / Caption:</td> 
		<td class="cms_form_element" align="left">
		  <textarea name="shortdescription" id="cms_form_shortdescription" class="cms_form_textarea"></textarea>
		</td>
	  </tr>
	  <tr id="cms_masteredit_220_11362" class="cms_masteraddedit_table"> 
	    <td id="cms_form_label_content" class="cms_form_label" >Image / File:</td> 
	    <td class="cms_form_element" align="left">
		  <input type="hidden" name="content" value=",," /><input type="file" name="content" id="cms_form_content" class="cms_form_file"></input>
		</td>
	  </tr>
<tr class="cms_masteraddedit_table">
<td class="cms_form_label">
Files:
</td>
<td align="left" class="cms_form_element">
<!-- This is where the output will appear -->
<div id="files_list"></div>
<script>
	<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
	var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 10 );
	<!-- Pass in the file element -->
	multi_selector.addElement( document.getElementById( 'cms_form_content' ) );
</script>
</td>
</tr>
	  <tr>
	    <td colspan="2" align="right" valign="middle">
		  <script language="javascript" type="text/javascript"> function selectAllElementsInSelect( select ) { for (i=0; i < select.options.length; i++){ select.options[i].selected = true; } return; } function doOnSubmitForElementsOnForm( formName ){ f = document.getElementById( formName ); if (f != null){ for (j = 0; j < f.length; j++ ){ e = f[j]; if (e.onsubmit){ e.onsubmit(); } } } return; } </script>	            <a id="cms_form_submit_220" class="cms_form_submit" href="javascript:doOnSubmitForElementsOnForm('Files Module');document.forms['Files Module'].submit();" ><img src="/cms/images/button_go.gif" /></a>
		</td>
	  </tr>
	</table>
</form>
</div>
<?
}
?>
